package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.Statistics;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IStatisticsDao;
import com.jilefojing.service.ISourceService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月19日 20时40分29秒
 */
@Service
public class StatisticsService extends BaseService<Statistics> implements
		IStatisticsService {
	@Autowired
	private IStatisticsDao statisticsDao;
	@Autowired
	private ISourceService sourceService;

	@Override
	public EnhancedStatistics getEnhancedStatistics(
			ItemTypeEnumForSource itemType, long itemId, UserContext userContext) {
		List<EnhancedStatistics> enhancedStatistics = this
				.getEnhancedStatisticses(itemType,
						Arrays.asList(new Long[] { itemId }), userContext);
		if (!Detect.notEmpty(enhancedStatistics)) {
			return null;
		}
		return enhancedStatistics.get(0);
	}

	@Override
	public List<EnhancedStatistics> getEnhancedStatisticses(
			ItemTypeEnumForSource itemType, List<Long> itemIds,
			UserContext userContext) {
		List<Statistics> statisticses = statisticsDao.getListByItems(itemType,
				itemIds);

		List<BaseEnhanced> enhancedItems = sourceService
				.getEnhancedItemsNotAppend(itemType, itemIds, userContext);

		List<BaseEnhanced> notEntityEnhancedItems = new ArrayList<BaseEnhanced>();

		if (Detect.notEmpty(enhancedItems)) {
			for (BaseEnhanced _EnhancedItem : enhancedItems) {
				if (!_EnhancedItem.getBoolean("isEntity")) {
					notEntityEnhancedItems.add(_EnhancedItem);
				}
			}
		}

		if (Detect.notEmpty(notEntityEnhancedItems)) {
			sourceService.appendEnhancedChildren2EnhancedItems(
					notEntityEnhancedItems, userContext);

			Map<Long, List<Long>> notEntityMaps = new HashMap<Long, List<Long>>();
			for (BaseEnhanced _EnhancedItem : notEntityEnhancedItems) {
				List<BaseEnhanced> enhancedChildred = (List<BaseEnhanced>) _EnhancedItem
						.get("getEnhancedChild"
								+ itemType.getItemClass().getSimpleName() + "s");

				if (Detect.notEmpty(enhancedChildred)) {
					List<Long> childIds = new ArrayList<Long>();
					for (BaseEnhanced _EnhancedChild : enhancedChildred) {
						childIds.add(_EnhancedChild.getDynamicId());
					}
					notEntityMaps.put(_EnhancedItem.getDynamicId(), childIds);

				}
			}
			if (Detect.notEmpty(notEntityMaps)) {
				List<Statistics> notEntityStatisticses = statisticsDao
						.getListForNotEntitys(itemType, notEntityMaps);
				if (Detect.notEmpty(notEntityStatisticses)) {
					if (statisticses == null) {
						statisticses = new ArrayList<Statistics>();
					}
					statisticses.addAll(notEntityStatisticses);
				}
			}

		}

		if (!Detect.notEmpty(statisticses)) {
			return null;
		}

		return this.conventStatisticses2EnhancedStatisticses(statisticses);
	}

	@Override
	public List<EnhancedStatistics> conventStatisticses2EnhancedStatisticses(
			List<Statistics> statisticses) {
		List<EnhancedStatistics> enhancedStatistics = new ArrayList<EnhancedStatistics>();
		for (Statistics _Statistics : statisticses) {
			enhancedStatistics.add(new EnhancedStatistics(_Statistics));
		}
		return enhancedStatistics;
	}

	@Override
	public void updateCount(ItemTypeEnumForSource itemType, long itemId,
			String updateColumn, boolean plus, int value,
			UserContext userContext) {
		EnhancedStatistics enhancedStatistics = this.getEnhancedStatistics(
				itemType, itemId, userContext);

		if (enhancedStatistics != null) {
			statisticsDao.updateCount(enhancedStatistics.getId(), updateColumn,
					plus, value);
		} else {
			if (plus) {
				Statistics statistics = new Statistics();
				statistics.setItemId(itemId);
				statistics.setItemType(itemType.getCode());
				if (updateColumn.equals(Statistics.COLUMN_COUNT_PRAISE)) {
					statistics.setCountPraise(1L);
				} else if (updateColumn.equals(Statistics.COLUMN_COUNT_COMMENT)) {
					statistics.setCountComment(1L);
				} else if (updateColumn.equals(Statistics.COLUMN_COUNT_WATCH)) {
					statistics.setCountWatch(1L);
				} else if (updateColumn
						.equals(Statistics.COLUMN_COUNT_FAVORITE)) {
					statistics.setCountFavorite(1L);
				} else if (updateColumn.equals(Statistics.COLUMN_COUNT_WRITE)) {
					statistics.setCountWrite(1L);
				} else {// TODO
					throw new IllegalArgumentException("[表字段不支持]"
							+ updateColumn);
				}
				Date now = new Date();
				statistics.setCreateTime(now);
				statistics.setUpdateTime(now);
				statisticsDao.saveSelective(statistics);
			}
		}

	}

}
